<?php
/**
 * User: Eric-Nothing
 * Datetime: 2019/6/24-9:43
 */
namespace App\Controllers;

use App\Connector\ManageShapeInterface;
use App\Controller;
use App\Exception\AreaCalInvalidException;

class AreaCalculator extends Controller
{
    protected $shapes;

    public function __construct(array  $shapes)
    {
        $this->shapes = $shapes;
    }

    public function sum()
    {
        $area = [];

        //这样写违背开闭原则
//        foreach ($this->shapes as $shape){
//            if (is_a($shape,"App\Square")){
//                $area[] = pow($shape->length,2);
//            }elseif (is_a($shape,"App\Circle")){
//                $area[] = pi() * pow($shape->radius,2);
//            }
//        }
//

        //遵循开闭原则
        foreach ($this->shapes as $shape){

            if ($shape instanceof ManageShapeInterface){
                $area[] = $shape->cal();
                continue;
            }
            throw new AreaCalInvalidException;

        }

        return array_sum($area);

    }

    public function output()
    {
        return $this->sum();
    }

}